<?php


namespace App\Services;


use App\Models\BuildingInfo;
use App\Models\HouseBroker;

class BuildingService
{
    /**
     * @Notes:经纪人端获取楼市圈信息 是自己的信息带有删除按钮
     * @Author:Planb,
     * @Date: 2022/11/18,
     * @Time: 19:15,
     */
    public static function getBuildingInfo($uid){
        $buildInfo = BuildingInfo::orderby('created_at','desc')->get()->toArray();
        foreach ($buildInfo as $k=>$value){
            $buildInfoResult[$k]['data']=$value;
            if ($uid==$value['uid']){
                $buildInfoResult[$k]['myself'] = 'yes';
            }else{
                $buildInfoResult[$k]['myself'] = 'no';
            }
            $brokerInfo = HouseBroker::where('id',$value['uid'])->first(['id','broker_name','broker_image','broker_phone']);
            $buildInfoResult[$k]['userInfo'] = $brokerInfo;
        }
        if ($buildInfoResult){
            return $buildInfoResult;
        }else{
            return false;
        }
    }

    /**
     * @Notes:经纪人端删除楼市圈信息
     * @Author:Planb,
     * @Date: 2022/11/19,
     * @Time: 8:07,
     */
    public static function deleteBuildInfo($uid,$buildInfoId){
        $deleteResult = BuildingInfo::where('uid',$uid)->where('id',$buildInfoId)->delete();
        if ($deleteResult){
            $buildInfoResult = self::getBuildingInfo($uid);
            return $buildInfoResult;
        }else{
            return false;
        }
    }

    /**
     * @Notes:经纪人端发布楼市圈信息
     * @Author:Planb,
     * @Date: 2022/11/19,
     * @Time: 9:22,
     */
    public static function pushBuildInfo($uid,$message,$image){
        $result = BuildingInfo::insert([
           'uid'=>$uid,
            'message'=>$message,
            'image'=>$image,
            'created_at'=>date('Y-m-d H:i:s')
        ]);
        if ($result){
            $buildInfoResult = self::getBuildingInfo($uid);
            return $buildInfoResult;
        }else{
            return false;
        }
    }


    /**
     * @Notes:客户端获取楼市圈信息
     * @Author:Planb,
     * @Date: 2022/11/29,
     * @Time: 15:00,
     */
    public static function clientGetBuildInfo(){
        $buildInfo = BuildingInfo::with('brokerInfo')->orderBy('created_at','desc')->get();
        if ($buildInfo){
            return $buildInfo;
        }else{
            return false;
        }
    }
}
